package com.darker.yygh.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.darker.yygh.repository.HospitalRepository;
import com.darker.yygh.service.HospitalService;
import com.darker.yyghcmn.client.DictFeignClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private DictFeignClient dictFeignClient;

    //上传医院接口
    @Override
    public void save(Map<String, Object> map) {
        //讲map集合转换成对象方便操作
        String jsonString = JSON.toJSONString(map);
        Hospital hospital = JSON.parseObject(jsonString, Hospital.class);


        String hoscode = hospital.getHoscode();
        Hospital hospitalexist = hospitalRepository.findHospitalByHoscode(hoscode);
        //如果数据不存在则添加
        if (hospitalexist == null) {
            hospital.setStatus(1);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else {
            //如果数据存在则做修改操作
            hospital.setId(hospitalexist.getId());
            hospital.setStatus(0);
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }


    }

    //根据编号查询
    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.findHospitalByHoscode(hoscode);
        return hospital;
    }

    //根据医院编号获取医院预约挂号详情信息
    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> map = new HashMap<>();

        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
        map.put("hospital", hospital);
        map.put("bookingRule", hospital.getBookingRule());
        return map;
    }

    //根据医院名称查询医院集合
    @Override
    public List<Hospital> findByHosname(String hosname) {
        List<Hospital> hospitalList = hospitalRepository.getHospitalByHosnameLike(hosname);
        return hospitalList;
    }

    //根据hoscode获取医院名称
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospital.getHosname() != null) {
            return hospital.getHosname();
        }
        return "获取医院名称失败";
    }

    @Override
    public Map<String, Object> findHospById(String id) {
        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());
        Map<String, Object> map = new HashMap<>();
        map.put("hospital", hospital);
        map.put("bookingRule", hospital.getBookingRule());
        return map;
    }

    //更新医院状态值
    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospitalRepository.save(hospital);
    }

    //医院列表 （条件带分页查询）
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {

        Pageable pageable = PageRequest.of(page - 1, limit);
        ExampleMatcher matcher = ExampleMatcher.matching()

                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)

                //模糊查询
                .withIgnoreCase(true);//忽略大小写

        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);

        Example<Hospital> example = Example.of(hospital, matcher);

        Page<Hospital> all = hospitalRepository.findAll(example, pageable);
        all.getContent().stream().forEach(item -> {
            this.setHospitalHosType(item);
        });
        return all;
    }

    //封装医院等级，省，市，地区的数据
    private Hospital setHospitalHosType(Hospital hospital) {
        //根据dictCode和value获取医院等级名称
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        //查询省  市 地区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());


        hospital.getParam().put("hostypeString", hostypeString);
        hospital.getParam().put("fullAddress", provinceString + cityString + districtString);

        return hospital;
    }


}
